let baziId;
/**
 * 点击之后的事件
 */
function addFont() {
    // 获取元素里面的制
    var text = this.innerText;
    // 变成相应的颜色
    for (let i in allChars) {
        // 选择框的天干
        let docChooseTian = document.getElementById("chooseTian");
        // 选择框的阳性地支
        let docChooseDiZhiYang = document.getElementById("chooseDiZhiYang");
        // 选择框的阴性地支
        let docChooseDiZhiYin = document.getElementById("chooseDiZhiYin");

        // 八字内容与数组一致，得出八字
        if (text == allChars[i].name) {
            // 此时点击八字的id
            if (this.id <= 8) {
                baziId = this.id;
            }
            // 判断是不是天干
            // 此时点击的是天干
            if (this.id <= 4) {
                // 显示天干
                docChooseTian.style.display = "block";
                // 点击之后别的不能动
                // 其他的关闭
                docChooseDiZhiYang.style.display = "none";
                docChooseDiZhiYin.style.display = "none";
            } else if (this.id > 4 && this.id <= 8) {
                docChooseTian.style.display = "none";
                // 如果天干为阳，id为单数,显示地支
                // 获取对应天干的阴阳属性
                let tianGan = findObjByDocText(document.getElementById(Number(this.id) - 4).innerText);
                // 关闭天干选项
                if (tianGan.isYan) {
                    docChooseDiZhiYang.style.display = "block";
                    // 关闭天干和阴的地支
                    docChooseDiZhiYin.style.display = "none";
                } else {
                    docChooseDiZhiYin.style.display = "block";
                    // 关闭天干和阳的地支
                    docChooseDiZhiYang.style.display = "none";
                }
                // 在选择框中选择字的时候
            } else if (this.id >= 11) {
                // 获取id
                let docBaZi = document.getElementById(baziId);
                let docChooseBox = document.getElementById(this.id);
                // 天干选完后到地支，反之亦然
                if (this.id <= 20) {
                    //
                    docChooseTian.style.display = "none";

                    if ((this.id & 1) == 1) {
                        docChooseDiZhiYang.style.display = "block";
                    } else {
                        docChooseDiZhiYin.style.display = "block";
                    }
                } else {
                    docChooseTian.style.display = "block";
                    if ((this.id & 1) == 1) {
                        docChooseDiZhiYang.style.display = "none";
                    } else {
                        docChooseDiZhiYin.style.display = "none";
                    }
                }
                // 天干只能改变天干
                chageChar(docBaZi, docChooseBox, allChars[i]);
                if (baziId <= 4) {
                    baziId = Number(baziId) + 4;
                } else if (baziId < 8 && baziId > 4) {
                    baziId = Number(baziId) - 3;
                } else if (baziId == 8) {
                    // 选择框消失
                    docChooseTian.style.display = "none";
                    docChooseDiZhiYang.style.display = "none";
                    docChooseDiZhiYin.style.display = "none";
                }

                chageChar(docBaZi, docChooseBox, allChars[i]);

            }

            this.style.color = displayColor.get(allChars[i].wuXing)
        }
    }
    // console.log(this.innerText);
    // 获取位置
    // console.log(this.id);
    // 弹出选择框

}
/**
 * 开始计算八字
 */
function caleBaZi() {
    // 获取空亡
    kongWangToDoc();
    // 获取十神,年干的六亲
    tenGoldToDoc(document.getElementById("1").innerText, document.getElementById("3").innerText, "tenGodYear");
    tenGoldToDoc(document.getElementById("2").innerText, document.getElementById("3").innerText, "tenGodMonth");
    tenGoldToDoc(document.getElementById("4").innerText, document.getElementById("3").innerText, "tenGodHour");
    // 修改性别
    sexToDoc();
    // 计算格局
    patternToDoc();
}

/**
 * 变更格局
 */
function patternToDoc() {
    // 获取八字的对象
    let nianGan = findObjByDocText(document.getElementById("1").innerText);
    let yueGan = findObjByDocText(document.getElementById("2").innerText);
    let riGan = findObjByDocText(document.getElementById("3").innerText);
    let shiGan = findObjByDocText(document.getElementById("4").innerText);
    let nianZhi = findObjByDocText(document.getElementById("5").innerText);
    let yueZhi = findObjByDocText(document.getElementById("6").innerText);
    let riZhi = findObjByDocText(document.getElementById("7").innerText);
    let shiZhi = findObjByDocText(document.getElementById("8").innerText);

    // 月令被克制的次数
    let yueKeZhiCount = 0;
    // 1. 查空亡
    let kongWang = caleKongWang(riGan.name + riZhi.name);
    // 月令为空
    if (kongWang.indexOf(yueZhi.name) == 0) {
        yueZhi == null;
    }
    // 时支为空
    if (kongWang.indexOf(shiZhi.name) == 0) {
        shiZhi == null;
    }

    // 2. 查看日支是否两次受制
    // 日支被克制的次数
    // 月令不为空，看月令与日支；时支与日支的关系
    // 时支不为空
    if (shiZhi != null) {
        let r1;
        // 月令不为空，看月令与日支
        if (yueZhi != null) {
            r1 = diZhiRelation(yueZhi, riZhi);
            // 月令为空，看年支与日支的关系
        } else {
            r1 = diZhiRelation(nianZhi, riZhi);
        }
        // 月令与日支
        // 时支与日支
        let r2 = diZhiRelation(shiZhi, riZhi);
        // 日支被两次减力，日支无力
        if ("克泄刑冲克合".indexOf(r1) != -1 && "克泄刑冲克合".indexOf(r2) != -1) {
            riZhi = null;
        }
    }
    // 3. 月令是否两次受制，必须要有被“克，刑，冲，克合”
    // 确保日支有力
    if (riZhi != null) {
        // 月令不为空
        if (yueZhi != null) {
            // TODO
        }
    }
    // 4. 月干是否有根
    // 5. 时干是否有根
    if (shiZhi != null) {
        // TODO

    } else {
        // 此时"时支"，时干无根
        shiGan == null;
    }
    // 6. 用月令判断旺弱，空亡时用年柱，不旺不弱时


    // 7. 用月干，日支，时干判断格局


    document.getElementById("pattern").innerText = "开始计算格局";
}
/**
 * 修改性别
 */
function sexToDoc() {
    let sexSelect = document.getElementById("sexSelect");
    let sex = document.getElementById("sex");
    if (sexSelect.value == "man") {
        sex.innerText = "元男";
    } else if (sexSelect.value == "woman") {
        sex.innerText = "元女";
    }
}
/**
 * 
 * @param {*} baZiDoc  命盘doc
 * @param {*} chooseBoxDoc 选择框doc
 * @param {*} objChar 文字的队对象，用于改变颜色
 */
function chageChar(docBaZi, docChooseBox, objChar) {
    let objBaZi = findObjByDocText(docBaZi.innerText);
    let objChooseBox = findObjByDocText(docChooseBox.innerText);
    // 找到doc对应的对象
    if (objBaZi.isTianGan == objChooseBox.isTianGan) {
        // 字内容变
        docBaZi.innerText = docChooseBox.innerText;
        // 颜色改变
        docBaZi.style.color = displayColor.get(objChar.wuXing);
    }
}

/**
 * 
 * @param {根据doc文本，查询文本对应的对象} text 
 * @returns 
 */
function findObjByDocText(text) {
    // 找到doc对应的对象
    for (let i in allChars) {
        let name = allChars[i].name;
        if (name == text) {
            return allChars[i];
        }
    }
}
/**
 * 获取天干十神
 * @param {*} tianGan 天干
 * @param {*} riGan 日干
 * @param {*} tianGanTenGod 日干对应的十神id 
 */
function tenGoldToDoc(tianGan, riGan, tianGanTenGod) {
    let tenGod = caleTenGold(tianGan, riGan);
    let tianGanGodDoc = document.getElementById(tianGanTenGod);
    tianGanGodDoc.innerText = tenGod;
}
/**
 * 获取空亡
 */
function kongWangToDoc() {
    // 获取日柱
    let riZhu = document.getElementById("3").innerText + document.getElementById("7").innerText
    document.getElementById("kongWang").innerText = caleKongWang(riZhu);
}

/**
 * 给天干地支绑定点击事件
 */
const zhiGanList = document.getElementsByClassName("zhiGan");
for (var i in zhiGanList) {
    zhiGanList[i].onclick = addFont;
}


/**
 * 八字对象
 */
class baZiFonts {
    // 名字
    name;
    // 代表五行
    wuXing
    // 是否是天干,true为天干
    isTianGan;
    // 阴阳属性，true为阳
    isYan;
    // 旺弱,true为旺，默认为true
    isStrong;

    constructor(name, wuXing, isTianGan, isYan, isStrong) {
        this.name = name;
        this.wuXing = wuXing;
        this.isTianGan = isTianGan;
        this.isYan = isYan;
        this.isStrong = isStrong;
    }
}
const displayColor = new Map();
displayColor.set("木", "green");
displayColor.set("火", "red");
displayColor.set("土", "DarkGoldenRod");
displayColor.set("金", "gold");
displayColor.set("水", "blue");

/**
 * 定义八字中22个字的对象
 */
var allChars = [
    new baZiFonts("甲", "木", true, true, true)
    , new baZiFonts("乙", "木", true, false, true)
    , new baZiFonts("丙", "火", true, true, true)
    , new baZiFonts("丁", "火", true, false, true)
    , new baZiFonts("戊", "土", true, true, true)
    , new baZiFonts("己", "土", true, false, true)
    , new baZiFonts("庚", "金", true, true, true)
    , new baZiFonts("辛", "金", true, false, true)
    , new baZiFonts("壬", "水", true, true, true)
    , new baZiFonts("癸", "水", true, false, true)
    , new baZiFonts("子", "水", false, true, true)
    , new baZiFonts("丑", "土", false, false, true)
    // 地支
    , new baZiFonts("寅", "木", false, true, true)
    , new baZiFonts("卯", "木", false, false, true)
    , new baZiFonts("辰", "土", false, true, true)
    , new baZiFonts("巳", "火", false, false, true)
    , new baZiFonts("午", "火", false, true, true)
    , new baZiFonts("未", "土", false, false, true)
    , new baZiFonts("申", "金", false, true, true)
    , new baZiFonts("酉", "金", false, false, true)
    , new baZiFonts("戌", "土", false, true, true)
    , new baZiFonts("亥", "水", false, false, true)
];

/**
 * 空亡计算
 */
function caleKongWang(riZhu) {
    const map = new Map();
    map.set(["甲子", "乙丑", "丙寅", "丁卯", "戌辰", "己巳", "庚午", "辛未", "壬申", "癸酉"], "戌亥");
    map.set(["甲戌", "乙亥", "丙子", "丁丑", "戊寅", "己卯", "庚辰", "辛巳", "壬午", "癸未"], "申酉");
    map.set(["甲申", "乙酉", "丙戌", "丁亥", "戊子", "己丑", "庚寅", "辛卯", "壬辰", "癸巳"], "午未");
    map.set(["甲午", "乙未", "丙申", "丁酉", "戊戌", "己亥", "庚子", "辛丑", "壬寅", "癸卯"], "辰巳");
    map.set(["甲辰", "乙巳", "丙午", "丁未", "戊申", "己酉", "庚戌", "辛亥", "壬子", "癸丑"], "寅卯");
    map.set(["甲寅", "乙卯", "丙辰", "丁巳", "戊午", "己未", "庚申", "辛酉", "壬戌", "癸亥"], "子丑");
    for (var [key, value] of map) {
        if (key.indexOf(riZhu) != -1) {
            return value;
        }
    }
    return "日干错误";
}

/**
 * 十神计算
 * @param {*} gan1 为22个字，需要单独获取属性
 * @param {*} gan2 一般为日干
 */
function caleTenGold(gan1, gan2) {
    let objTianGan;
    let objRigan;
    for (let i in allChars) {
        // 获取日干对象
        if (gan1 == allChars[i].name) {
            objTianGan = allChars[i];
        }
        // 获取其他天干的对象
        if (gan2 == allChars[i].name) {
            objRigan = allChars[i];
        }
    }
    // 是否为同性
    let isSameArrt = (objRigan.isYan == objTianGan.isYan);
    // TODO
    // 获取日干属性
    // 相生
    let tenGod;
    switch (wuRelation(objTianGan.wuXing, objRigan.wuXing)) {
        case "生":
            tenGod = isSameArrt ? "偏印" : "正印";
            break;
        case "扶":
            tenGod = isSameArrt ? "比肩" : "劫财";
            break;
        case "克":
            tenGod = isSameArrt ? "偏财" : "正财";
            break;
        case "泄":
            tenGod = isSameArrt ? "食神" : "伤官";
            break;
        case "耗":
            tenGod = isSameArrt ? "偏官" : "正官";
            break;

        default:
            break;
    }
    return tenGod;
}

/**
 *
 * @param {出力的一方，为中文} gan1
 * @param {收到力的一方，为中文} gan2
 * @returns
 */
function wuRelation(gan1, gan2) {
    let guanXi = gan1 + gan2;
    if (gan1 == gan2) {
        return "扶"
    }
    // 获取属性的能量
    if ("木火" == guanXi ||
        "火土" == guanXi ||
        "土金" == guanXi ||
        "金水" == guanXi ||
        "水木" == guanXi
    ) {
        return "生";
    }
    if ("火木" == guanXi ||
        "土火" == guanXi ||
        "金土" == guanXi ||
        "水金" == guanXi ||
        "木水" == guanXi
    ) {
        return "泄";
    }
    if ("火金" == guanXi ||
        "土水" == guanXi ||
        "金木" == guanXi ||
        "水火" == guanXi ||
        "木土" == guanXi
    ) {
        return "克";
    }
    if ("金火" == guanXi ||
        "水土" == guanXi ||
        "木金" == guanXi ||
        "火水" == guanXi ||
        "土木" == guanXi
    ) {
        return "耗";
    }
}

/**
 * 地支之间的关系
 * @param {出力的一方，为对象} dizhi1
 * @param {受力的一方，为对象} dizhi2
 * @returns {生，扶，克，泄，耗，克合，生合，冲，刑},其中：冲，刑，克合是双方减力，与克的威力一样，在判断格局的时候，地支被耗不减力
 */
function diZhiRelation(dizhi1, dizhi2) {
    // 五行的关系
    let relation = wuRelation(dizhi1.wuXing, dizhi2.wuXing);
    // 特殊情况
    let str = dizhi1.name + dizhi2.name;
    // 1. 丑戌刑
    if ("丑戌" == str || "戌丑" == str) {
        return "刑";
    }
    // 地支六合？
    if (("子丑" == str || "丑子" == str)
        || ("巳申" == str || "申巳" == str)
        || ("卯戌" == str || "戌卯" == str)
    ) {
        // 双方减力
        return "克合";
    }
    if (("寅亥" == str || "亥寅" == str)
        || ("辰酉" == str || "酉辰" == str)
        || ("午未" == str || "未午" == str)
    ) {
        // 双方减力
        return "生合";
    }
    // 地支六冲
    if (("子午" == str || "午子" == str)
        || ("寅申" == str || "申寅" == str)
        || ("辰戌" == str || "戌辰" == str)
        || ("卯酉" == str || "酉卯" == str)
        || ("丑未" == str || "未丑" == str)
        || ("巳亥" == str || "亥巳" == str)
    ) {
        // 双方减力
        return "冲";
    }

    // 第一阶段， 辰丑生申酉金，不克亥子水。而帮亥子水
    if ((str == "辰子" || str == "子辰")
        || (str == "辰亥" || str == "亥辰")
        || (str == "丑亥" || str == "亥丑")
    ) {
        return "帮"
    }
    // 第一阶段，未戌土不泄巳午火
    if ((str == "未巳" || str == "巳未")
        || (str == "未午" || str == "午未")
        || (str == "戌巳" || str == "巳戌")
        || (str == "戌午" || str == "午戌")
    ) {
        return "帮"
    }
    // 未戌土克亥子水，也克申酉金
    if (str == "未申" || str == "未酉"
        || str == "戌申" || str == "戌酉"
    ) {
        return "克"
    }
    if (str == "申未" || str == "酉未"
        || str == "申戌" || str == "酉戌"
    ) {
        return "耗"
    }
    return relation;
}


/**
 * 判断地支是不是天干的根
 * @param {地支对象} diZhi
 * @param {天干对象} tianGan
 */
function hasRoot(diZhi, tianGan) {
    let wuRealt = wuRelation(diZhi.name, tianGan.name);

}
